import 'dart:math';

import 'package:flutter/material.dart';

class StateManagementPage extends StatefulWidget {
  final Map<String, dynamic> arguments;
  StateManagementPage({Key key, this.arguments}) : super(key: key);

  @override
  _StateManagementPageState createState() => _StateManagementPageState();
}

class _StateManagementPageState extends State<StateManagementPage> {
  int _apples = 0;

  int oranges = 0;
  VoidCallback orangesAdd() {
    setState(() {
      this.oranges++;
    });
    return null;
  }

  int strawberries = 0;
  VoidCallback strawberryAdd() {
    setState(() {
      this.strawberries++;
    });
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('StateManagementPage'),
      ),
      body: ListView(
        children: <Widget>[
          // StatelessSample
          ListTile(
            title: Text('StatelessSample'),
            subtitle: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Text('''
- StatelessWidget 实例必须是 filal

class StatelessSample extends StatelessWidget {
  final String name;
  const StatelessSample({Key key, this.name}) : super(key: key);

  @override
  Widget build(BuildContext context) {
...'''),
                ),
                Divider(height: 20.0, endIndent: 200),
                StatelessSample(
                    apples: this._apples,
                    add: () {
                      setState(() {
                        this._apples = Random().nextInt(10000);
                      });
                    })
              ],
            ),
          ),
          Divider(height: 1.0),
          // StatefulWidgetSample
          ListTile(
            title: Text('StatefulWidgetSample'),
            subtitle: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Text('''
状态更新使用:
setState(() {

});'''),
                ),
                Divider(height: 20.0, endIndent: 200),
                StatefulWidgetSample(),
                Divider(height: 20.0, endIndent: 200),
              ],
            ),
          ),
          Divider(height: 1.0),
          ListTile(
              title: Text('InheritedWidget'),
              subtitle: FruitsAddInherited(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SingleChildScrollView(
                      scrollDirection: Axis.horizontal,
                      child: Text('''
- 解决层级深的传值问题;
- 放于某一节点, 该节点下的子节点可以直接访问;
- 类似于: Theme.of(context).textTheme.title;
- 实际开发中, 将 FruitsAddInherited 示例中的各个数据统一再一层 Model 包装<事件除外>;'''),
                    ),
                    Divider(height: 20.0, endIndent: 200),
                    OrangeList(),
                  ],
                ),
                oranges: this.oranges,
                orangesAdd: this.orangesAdd,
                strawberries: this.strawberries,
                strawberryAdd: this.strawberryAdd,
              )),
          Divider(height: 1.0),
          ListTile(
            // TODO:
            title: Text('三方库 ScopedModel <后续学习>'),
          ),
          Divider(height: 1.0),
        ],
      ),
    );
  }
}

/// StatelessSample
class StatelessSample extends StatelessWidget {
  final int apples;
  final VoidCallback add;
  const StatelessSample({Key key, @required this.apples, @required this.add}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('apples: ' + this.apples.toString()),
          Container(
            alignment: Alignment.bottomRight,
            child: RaisedButton(
              child: Text('apples Add'),
              onPressed: () {
                this.add();
              },
            ),
          )
        ],
      ),
    );
  }
}

/// StatefulWidgetSample
class StatefulWidgetSample extends StatefulWidget {
  StatefulWidgetSample({Key key}) : super(key: key);

  @override
  _StatefulWidgetSampleState createState() => _StatefulWidgetSampleState();
}

class _StatefulWidgetSampleState extends State<StatefulWidgetSample> {
  int _bananas = 0; // 为该 Widget 内部管理的状态
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('Widget 内部自已管理的状态 _bananas: ${this._bananas}'),
          Container(
            alignment: Alignment.bottomRight,
            child: RaisedButton(
              child: Text('_bananas Add'),
              onPressed: () {
                setState(() {
                  this._bananas++;
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}

/// InheritedWidget 的使用
class FruitsAddInherited extends InheritedWidget {
  final Widget child;

  final int oranges;
  final VoidCallback orangesAdd;

  final int strawberries;
  final VoidCallback strawberryAdd;

  FruitsAddInherited({this.child, this.oranges, this.orangesAdd, this.strawberries, this.strawberryAdd}) : super(child: child);

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    return true;
  }

  static FruitsAddInherited of(BuildContext context) => context.dependOnInheritedWidgetOfExactType(aspect: FruitsAddInherited);
}

class OrangeList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        OrangeInfo(),
        StrawberryInfo(),
      ],
    );
  }
}

class OrangeInfo extends StatelessWidget {
  // 使用 InheritedWidget 后, 以下需要来自上级节点的实例数据 及 构造函数 可以去除

  // final int oranges;
  // final VoidCallback orangesAdd;
  // const OrangeInfo({Key key, @required this.oranges, @required this.orangesAdd}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final oranges = FruitsAddInherited.of(context).oranges;
    final orangesAdd = FruitsAddInherited.of(context).orangesAdd;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text('OrangeInfo oranges: ' + oranges.toString()),
        Container(
          alignment: Alignment.bottomRight,
          child: RaisedButton(
            child: Text('oranges Add'),
            onPressed: () {
              orangesAdd();
            },
          ),
        ),
      ],
    );
  }
}

class StrawberryInfo extends StatelessWidget {
  const StrawberryInfo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final strawberries = FruitsAddInherited.of(context).strawberries;
    final strawberryAdd = FruitsAddInherited.of(context).strawberryAdd;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text('StrawberryInfo oranges: ' + strawberries.toString()),
        Container(
          alignment: Alignment.bottomRight,
          child: RaisedButton(
            child: Text('strawberry Add'),
            onPressed: () {
              strawberryAdd();
            },
          ),
        ),
      ],
    );
  }
}
